using Godot;
using System;
using Array = Godot.Collections.Array;
public class View : Control
{
    Camera2D camera;
    RichTextLabel Info;
    public HotBar hotBar;
    public StoreView OpenView;
    public Console console;
    public Slot CursorSlot;
    public Label ItemNameTag, Lore;
    public HpBar hpBar;
    public Sprite Layer;
    public StatePanel statePanel;
    public BlockDataInfo blockDataInfo;
    public override void _Ready()
    {
        hpBar = GetNode<HpBar>("HpBar");
        camera = GetParent().GetNode<Camera2D>("Camera2D");
        ItemNameTag = GetNode<Label>("CurosrSlot/NameTag");
        Lore = GetNode<Label>("CurosrSlot/NameTag/Lore");
        Info = GetNode<RichTextLabel>("Info");
        hotBar = GetNode<HotBar>("HotBar");
        OpenView = GetOpenView();
        console = GetNode<Console>("Console");
        CursorSlot = GetNode<Slot>("CurosrSlot");
        Layer = GetNode<Sprite>("Layer/sprite");
        statePanel = GetNode<StatePanel>("StatePanel");
        blockDataInfo = GetNode<BlockDataInfo>("BlockDataInfo");
    }

    public override void _Process(float delta)
    {
        Vector2 ofs = (GetViewport().Size * camera.Zoom / 2);
        this.MarginLeft = -ofs.x;
        this.MarginTop = -ofs.y;
        this.MarginRight = ofs.x;
        this.MarginBottom = ofs.y;
    }
    public View AddInfo(String str)
    {
        Info.BbcodeText += $"{str}\n";
        return this;
    }
    public View ClearInfo()
    {
        Info.Clear();
        Info.BbcodeText = "";
        return this;
    }
    public bool GetInfoVisible() { return Info.Visible; }
    public void SetInfoVisible(bool b) { Info.Visible = b; }
    public void CloseOpenView()
    {
        OpenView.QueueFree();
        OpenView = null;
        ItemNameTag.Visible = false;
        Lore.Visible = false;
    }
    public void UpdataOpenView(Player player)
    {
        if (OpenView != null)
        {
            OpenView.Updata(player);
        }
    }
    //设置打开容器
    public void SetOpenView(StoreView sv)
    {
        ItemNameTag.Text = "";
        Lore.Text = "";
        ItemNameTag.Visible = true;
        Lore.Visible = true;
        sv.SetPivotOffset(new Vector2(93, 32));
        sv.SetScale(new Vector2(1f, 1f));
        OpenView = sv;
        AddChild(sv);
        sv.Init(BlockWorld.player);
        MoveChild(CursorSlot, GetChildCount() - 1);
    }
    public void Close()
    {
        Array chides = GetChildren();
        for (int i = 0; i < chides.Count; i++)
        {
            if (chides[i] is StoreView)
            {
                ((StoreView)chides[i]).QueueFree();
            }
        }
    }
    public StoreView GetOpenView()
    {
        Array chides = GetChildren();
        for (int i = 0; i < chides.Count; i++)
        {
            if (chides[i] is StoreView)
            {
                return (StoreView)chides[i];
            }
        }
        return null;
    }
    //是否有打开容器
    public bool HasOpenView()
    {
        if (OpenView != null) return true;
        else return false;
    }
}
